from sqlobject import *
from sqlobject.sqlbuilder import *

class Item(SQLObject):
	itemId = IntCol(notNone = True, alternateID = True)
	name = UnicodeCol(notNone = True)
	itemLevel = IntCol(notNone = True)
	quality = IntCol(notNone = True)
	icon = UnicodeCol(notNone = True)

class ItemHistory(SQLObject):
	date = DateTimeCol(default = None)
	item = ForeignKey("Item", notNone = True)
	volume = IntCol(default = 0)
	avgPpuBuy = IntCol(default = None)
	avgPpuBid = IntCol(default = None)
	stdDevPpuBuy = IntCol(default = None)
	stdDevPpuBid = IntCol(default = None)

class Auction(SQLObject):
	auctionId = IntCol(notNone = True, alternateID = True)
	seller = UnicodeCol(notNone = True)
	item = ForeignKey("Item", notNone = True)
	seed = IntCol(default = None)
	quantity = IntCol(notNone = True)
	buy = IntCol(default = None)
	bid = IntCol(default = None)
	ppuBuy = IntCol(default = None)
	ppuBid = IntCol(default = None)

class SearchParam(SQLObject):
	filterId = UnicodeCol(default = None)
	minLvl = IntCol(default = None)
	maxLvl = IntCol(default = None)
	qual = IntCol(default = None)
	active = BoolCol(default = True)

Filters = [
	"0,78","0,82","0,41","0,64","0,17","0,12","0,62","0,23","0,48","0,61","0,35",
	"0,92","0,15","0,21","0,49","0,42","0,33","1,34,119","1,34,120","1,34,121",
	"1,34,122","1,34,123","1,34,124","1,43,125","1,43,126","1,43,127","1,43,128",
	"1,43,129","1,43,130","1,43,131","1,43,132","1,43,133","1,76,134","1,76,135",
	"1,76,136","1,76,137","1,76,138","1,76,139","1,76,140","1,76,141","1,83,142",
	"1,83,143","1,83,144","1,83,145","1,83,146","1,83,147","1,83,148","1,83,149",
	"1,86,150","1,86,151","1,86,152","1,86,153","1,86,154","1,86,155","1,86,156",
	"1,86,157","1,67","1,74","1,60","1,53","1,56","2,57","2,26","2,16","2,20",
	"2,24","2,97","2,94","2,29","2,37","3,100","3,102","3,104","3,109","3,46",
	"3,108","3,106","3,110","4,66","4,80","4,88","4,65","4,84","4,71","4,93","4,30",
	"4,28","4,55","5,112","5,114","5,113","5,111","5,99","5,101","5,103","5,105",
	"5,107","5,32","5,116","5,90","5,115","5,117","5,118","6,22","6,19","7,77",
	"7,68","8,58","8,44","8,75","8,95","8,70","8,14","8,45","8,59","8,54","8,47",
	"8,51","8,13","9,73","9,50","9,52","9,25","9,96","9,18","9,27","9,98","9,89",
	"10,39","10,31","10,91","10,38","10,63","10,69","11",
]

def init_search():
	for filter in Filters:
		SearchParam(filterId = filter)


def init(uri = None):
	if not uri:
		import os
		CONFIG_FILE = os.path.join(os.environ['HOME'], '.wjs.ini')
		from ConfigParser import SafeConfigParser as ConfigParser
		config = ConfigParser()
		config.read(CONFIG_FILE)
		uri = config.get("dsn", "auctions")
	global connection
	connection = connectionForURI(uri)
	sqlhub.processConnection = connection
	Item.createTable(ifNotExists = True)
	Auction.createTable(ifNotExists = True)
	SearchParam.createTable(ifNotExists = True)
	ItemHistory.createTable(ifNotExists = True)


